import logging
import os
import time

from frame.common.global_data.path_param import Param
from frame.common.util.config_util import config as yml



class Logging:
    """
    初始化日志配置
    """

    def __init__(self):
        # 输出日志格式
        formatter = logging.Formatter(yml.get_log_value("formatter"))

        #

        # 日志路径
        self.log_path = os.path.join(
            Param.get_value("path"),
            yml.get_log_value("path"),
            'all_logs_{}.log'.format(time.strftime('%Y-%m-%d')))
        self.logger = logging.getLogger()
        self.logger.setLevel(yml.get_log_value("level"))

        # 文件输出控制器
        self.fh = logging.FileHandler(filename=self.log_path, encoding='utf-8')
        self.fh.setLevel(yml.get_log_value("level"))
        self.fh.setFormatter(formatter)

        # 控制台输出控制器
        self.ch = logging.StreamHandler()
        self.ch.setLevel(yml.get_log_value("level"))
        self.ch.setFormatter(formatter)

        self.logger.addHandler(self.fh)
        self.fh.close()
        self.logger.addHandler(self.ch)
        self.ch.close()

    def logging(self):
        return logging

    @staticmethod
    def __getLogLevel():
        return logging.INFO


# logging实例对象
logger = Logging().logging()
